
package cn.dankal.flyaudio.information.detail;


import androidx.annotation.NonNull;

import java.util.HashMap;

import api.InfoServiceFactory;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.BasePresenterImp;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.rx.AbstractStateSubscriber;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.BaseBeanList;
import cn.dankal.business.pojo.CommentBean;
import cn.dankal.business.pojo.InfoBean;
import cn.dankal.business.pojo.ReasonBean;
import io.reactivex.disposables.Disposable;

import static cn.dankal.basiclib.base.fragment.BaseRecyclerViewFragment.PAGE_SIZE;

public class InformationPresenter extends BasePresenterImp<InformationContract.View> implements InformationContract.Presenter {

    private long lastCommitClickTime;// 上次点击评论的时间
    private String lastCommitClickUuid;// 上次点击评论的uuid
    private long lastPraiseClickTime;// 上次点赞的时间
    private String lastPraiseUuid;// 上次点赞的uuid
    // 暴力点击的阻隔时间 单位：ms
    private long barrierTime = 1000L;


    private String uuid;

    private InformationContract.View view;
    private int page = 1;

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Override
    public void attachView(@NonNull InformationContract.View view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        view = null;
    }

    @Override
    public void praise(String uuid, String commentType, int position) {
        // 防止暴力点击 防止相同的位置重复点击
        if (lastPraiseUuid != null && lastPraiseUuid.equals(uuid) && lastPraiseClickTime + barrierTime > System.currentTimeMillis()) {
            lastPraiseClickTime = System.currentTimeMillis();
            return;
        } else {
            lastPraiseClickTime = System.currentTimeMillis();
            lastPraiseUuid = uuid;
        }
        InfoServiceFactory
                .praise_user_info("2", uuid, true)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(view) {

                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        InformationPresenter.this.view.onPraiseResult(reasonBean, position);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                });
    }

    @Override
    public void praiseCancel(String uuid, String commentType, int position) {
        // 防止暴力点击 防止相同的位置重复点击
        if (lastPraiseUuid != null && lastPraiseUuid.equals(uuid) && lastPraiseClickTime + barrierTime > System.currentTimeMillis()) {
            lastPraiseClickTime = System.currentTimeMillis();
            return;
        } else {
            lastPraiseClickTime = System.currentTimeMillis();
            lastPraiseUuid = uuid;
        }
        InfoServiceFactory
                .praise_user_info("2", uuid, false)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(view) {

                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        InformationPresenter.this.view.onPraiseResult(reasonBean, position);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }
                });
    }

    @Override
    public void refreshCommitList() {
        page = 1;
        getCommentList(
                uuid, page++, Integer.valueOf(PAGE_SIZE));
    }

    @Override
    public void getCommentList(String uuid,
                               Integer page_index, Integer page_size) {
        InfoServiceFactory
                .comment_list(uuid, page_index + "", PAGE_SIZE)
                .subscribe(new AbstractDialogSubscriber<BaseBeanList<CommentBean>>(view) {

                    @Override
                    public void onNext(BaseBeanList<CommentBean> reasonBean) {

                        InformationPresenter.this.view.onCommentList(reasonBean);
                    }
                });


    }

    @Override
    public void onRefresh() {
        page = 1;
        onLoadMore();
    }

    @Override
    public void onLoadMore() {
        getCommentList(
                uuid,
                page++, Integer.valueOf(PAGE_SIZE));
    }

    @Override
    public void comment(String content, String uuid) {
        // 防止暴力点击 防止相同的位置重复点击
        if (lastCommitClickUuid != null && lastCommitClickUuid.equals(uuid) && lastCommitClickTime + barrierTime > System.currentTimeMillis()) {
            lastCommitClickTime = System.currentTimeMillis();
            return;
        } else {
            lastCommitClickTime = System.currentTimeMillis();
            lastCommitClickUuid = uuid;
        }
        if (content == null || content.isEmpty()) {
            ToastUtils.showShort("评论内容不能为空.");
            return;
        }
        InfoServiceFactory
                .comment(new HashMap<String, String>(2) {{
                    put("content", content);
                    put("uuid", uuid);
                }})
                .subscribe(new AbstractSubscriber<ReasonBean>(view) {

                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        InformationPresenter.this.view.onCommentResult(reasonBean, new CommentBean());
                    }
                });
    }


    @Override
    public void inside_comment(String commentUuid, String content,
                               String receiveUuid, String uuid) {
        if (content == null || content.isEmpty()) {
            ToastUtils.showShort("评论内容不能为空.");
            return;
        }
        InfoServiceFactory
                .inside_comment(new HashMap<String, Object>(3) {{
                    put("content", content);
                    put("commentUuid", commentUuid);
                    put("receiveUuid", receiveUuid);
                    put("uuid", uuid);
                }})
                .subscribe(new AbstractSubscriber<ReasonBean>(view) {

                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        // 将原来的上传的 评论 返回原来的列表进行局部刷新
                        CommentBean newComment = new CommentBean();
                        newComment.content = content;
                        newComment.uuid = uuid;
                        newComment.name = DKUserManager.getUserInfo().getName();
                        InformationPresenter.this.view.onCommentResult(reasonBean, newComment);
                    }
                });
    }

    @Override
    public void info_detail(String uuid) {
        InfoServiceFactory.info_details(uuid)
                .subscribe(new AbstractStateSubscriber<InfoBean>(view) {
                    @Override
                    public void onNext(InfoBean infoBean) {
                        super.onNext(infoBean);
                        InformationPresenter.this.view.onInfoDetailResult(infoBean);
                    }
                });
    }

//    @Override
//    public void current_detail(String uuid) {
//        InfoServiceFactory.info_details(uuid)
//                .subscribe(new AbstractStateSubscriber<InfoBean>(view){
//                    @Override
//                    public void onNext(InfoBean currentBean) {
//                        super.onNext(currentBean);
//                        InformationPresenter.this.view.onInfoDetailResult(currentBean);
//                    }
//                });
//    }

}
